<!--  -->
<template>
  <div id="main">
    <h2>信用卡银行管理</h2>
    <el-button type="primary" style="margin:20px" @click="handleOpenDialog('添加信用卡银行',1)">添加信用卡银行</el-button>
    <div id="table-area">
      <el-table
        v-loading="listLoading"
        :data="list.slice((currentPage-1)*pagesize,currentPage*pagesize)"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >
        <!-- <el-table-column
          align="center"
          prop="bankId"
          label="银行编号"
          min-width="10%"
        /> -->

        <el-table-column
          label="序号"
          type="index"
          min-width="10%"
          align="center"
        >
          <template scope="scope">
            <span>{{ (currentPage - 1) * pagesize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          prop="creditBankName"
          label="银行名称"
          min-width="40%"
        />

        <el-table-column
          align="center"
          prop="creditBankPic"
          label="银行图片"
          min-width="10%"
        >
          <template slot-scope="Scope">
            <img :src="Scope.row.creditBankPic" width="100px" height="100px">
          </template>
        </el-table-column>

        <el-table-column align="center" label="操作" min-width="35%">
          <template slot-scope="Scope">
            <el-button type="primary" icon="el-icon-edit" @click="handleOpenDialog('修改银行',2,Scope.row.creditBankId)">查看/修改</el-button>
            <el-button type="danger" icon="el-icon-delete" @click="handleDelete(Scope.row.creditBankId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

    </div>
    <div id="pages">
      <el-pagination
        :current-page="currentPage"
        :page-sizes="[5,10]"
        :page-size="pagesize"
        layout="total, sizes,prev, pager, next"
        :total="list.length"
        prev-text="上一页"
        next-text="下一页"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <creditBankDetail :id="id" :title="title" :type="type" :visible="dialogFormVisible" @close="handleClose" />
  </div>
</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// 例如：import 《组件名称》 from '《组件路径》';
import { getAllCreditBank, deleteCreditBank } from '@/api/creditBank'
import creditBankDetail from '@/views/creditBank/creditBankDetail'
export default {
  name: 'CreditBank',
  // import引入的组件需要注入到对象中才能使用
  components: {
    'creditBankDetail': creditBankDetail
  },
  data() {
    // 这里存放数据
    return {
      currentPage: 1, // 默认显示页面为1
      pagesize: 5, //    每页的数据条数
      list: [],
      listLoading: false,
      editType: 1, // 1 添加 2 修改
      url: process.env.VUE_APP_BASE_API + 'file/',
      id: 0,
      title: '',
      type: 1,
      dialogFormVisible: false
    }
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {},
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.fetchData()
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {

  },
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {},
  // 方法集合
  methods: {
    // 每页下拉显示数据
    handleSizeChange: function(size) {
      this.pagesize = size
    },
    // 点击第几页
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage
    },
    handleClick(row) {
      console.log(row)
    },
    fetchData() {
      this.mohu = ''
      getAllCreditBank().then((resp) => {
        this.list = resp.obj
      })
    },
    handleDelete(id) {
      this.$confirm('确认删除?').then(() => {
        deleteCreditBank(id).then(() => {
          this.$notify({
            title: '操作成功',
            type: 'success'
          })
          const totalPage = Math.ceil((this.list.length - 1) / this.pagesize)
          const page = this.currentPage > totalPage ? totalPage : this.currentPage
          this.currentPage = this.currentPage < 1 ? 1 : page
          this.fetchData()
        })
      })
    },
    handleClose() {
      this.dialogFormVisible = false
    },
    handleOpenDialog(title, type, id) {
      this.title = title
      this.type = type
      if (type === 2) { this.id = id }
      this.dialogFormVisible = true
    }
  } // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style scoped>
#main{
    margin-left: 5px;
    margin-right: 5px;
}
#table-area{
    margin-left: 10px;
    margin-right: 10px;
}
</style>
